<?php

require_once app_path().'/pay/dysms/Sms.php';
use Illuminate\Support\Facades\Redirect;
use Illuminate\Support\Facades\Response;
//namespace controllers\admin;

class AdminOrderController extends BaseController {
	
	/**
	 * The layout that should be used for responses.
	 */
	protected $layout = 'layouts.admin';
	/**
	 * 
	 */
	public function showOrders(){
		//$page = Input::get('page',1);
		//paginate会自动处理分页数字,只查看已经支付的订单
		$query = Order::orderBy('id','desc');
		$name = Input::get("name");
		$addressName = Input::get("addressName");
		$addressTel = Input::get("addressTel");
		$search = array();
		if (!empty($name)){
			$query->where('user_name','like','%'.$name.'%');
			$search["name"] = $name;
		}
		
// 		$posts = Post::whereHas('comments', function($q)
// 		{
// 			$q->where('content', 'like', 'foo%');
		
// 		})->get();
		if (!empty($addressName) && !empty($addressTel)){
			$query->whereHas("address",function($sub) use($addressName,$addressTel){
				$sub->where('name', 'like', '%'.$addressName.'%');
				$sub->where('mobile', 'like', $addressTel.'%');
			});
		} else if (!empty($addressName)){
			$query->whereHas("address",function($sub) use($addressName,$addressTel){
				$sub->where('name', 'like', '%'.$addressName.'%');
			});
			
		} else if (!empty($addressTel)){
			$query->whereHas("address",function($sub) use($addressName,$addressTel){
				$sub->where('mobile', 'like', $addressTel.'%');
			});
		}
		
		
		$orders = $query->paginate(50);//
		// 		$products = Products::withTrashed()->orderBy('id','desc')->forPage(50,2)->get();
		$content = View::make('admin.order.orders');
		$this->layout->content = $content;
		$content->with("orders",$orders);
		$content->with("search",$search);
	}
	
	
	public function showOrder($id){
		//查看订单信息，产品信息，地址信息，card信息
		$content = View::make('admin.order.order-detail');
		$this->layout->content = $content;
		//$order = Order::with("address","card","products.product")->find($id);
		
		$order = Order::with(array("address","card","products.product" => function($query){
			$query->withTrashed();
		
		}))->find($id);
		$content->with("order",$order);
	}
	
	public function postDelivery(){
		$data = Input::all();
		
		$order = Order::with("user","address","card")->with(array("products.product"=>function($query){
			$query->withTrashed();
		}))->find($data["id"]);
		
		//更新订单
		$order->update($data);
		$data = array(
			'order'=>$order
		);
		
// 		Mail::queue('emails.delivery', $data, function($message) use ($order){
// 			$message->to($order->user->email, $order->user->name)->subject('您的订单'.$order->id."已发货");
// 		});

		$config = Config::get("mao.sms.config");
		$sms = new Sms($config);
		$templateCode = Config::get("mao.sms.shipping");
		$param = array(
			'orderNo' => $order->id . "",
			'expressNo' => $order->express_id,
			'expressName' => $order->express_name,
		);
		Log::info($order->user->mobile . 'tempalteCode=' . $templateCode);
		$sms->send($order->user->mobile,$templateCode,$param);
		return Redirect::route('order');
		//发货
	}
	
	public function postReturns(){
		//退货
		$id = Input::get('id');
		if (empty($id)){
			return Response::json(array('success'=>false));
		}
		$order = Order::find($id);
		if (empty($order)){
			return Response::json(array('success'=>false));
		}
		$data = Input::all();
		$order->recycle($data);
		//$order->status = 3;//退货
		//$order->save();
// 		return Response::json(array('success'=>true));

		//邮件通知内容
		$order = Order::with("user","address","card")->with(array("products.product"=>function($query){
			$query->withTrashed();
		}))->find($id);
		
		//更新订单
		$data = array(
				'order'=>$order
		);
// 		Mail::queue('emails.returns', $data, function($message) use ($order){
// 			$message->to($order->user->email, $order->user->name)->subject('您的订单'.$order->id."已退货");
// 		});
		
		$config = Config::get("mao.sms.config");
		$sms = new Sms($config);
		$templateCode = Config::get("mao.sms.receipt");
		$param = array(
				'orderNo' => $order->id . "",
		);
		$sms->send($order->user->mobile,$templateCode,$param);
		return Redirect::route('order');
	}
	
	public function postRemark(){
		$remark = Input::get("remark");
		$id = Input::get("id");
		$order = Order::find($id);
		$order->update(array('remark' => $remark));
		return Redirect::route('order');
	}
	
	public function postOfflinePay(){
		$remark = Input::get("remark");
		$id = Input::get("id");
		$order = Order::find($id);
		if ($order->status == 0){
			$order->pay_type = 3;
			$order->status = 1;
			if (!empty($remark)){
				$order->remark = $remark;
			}
			$order->save();
		}
		return Redirect::route('order');
	}
	
	
	
}

?>